package org.util

import grails.converters.JSON
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.DataUtils
import org.FileUtils

class JsonDataController {
    def dynamicQueryService

    def getEchartsLineData() {
        def echartType = params.type ?: 'hour'
        def fileName = "data/echart-line-" + echartType + ".json"
        def jsonSlurper = new JsonSlurper()
        render jsonSlurper.parseText(FileUtils.readLine(fileName)) as JSON
    }

    def getEchartsBarData() {
        def echartType = params.type ?: 'hour'
        def fileName = "data/echart-bar-" + echartType + ".json"
        def jsonSlurper = new JsonSlurper()
        render jsonSlurper.parseText(FileUtils.readLine(fileName)) as JSON
    }

    def getCassetteStatusData() {
        def cassetteStatusList = DataUtils.getCassetteStatusList()
        def cassetteStatusMap = [list: cassetteStatusList, selectPageSize: '10,20,50', pageCurrent: 1, showPagenum: 7, total: 1557]
        render cassetteStatusMap as JSON
    }

    def getOptionHTMLByTarget() {
        //sql 查询标准：select value,label
        def list = dynamicQueryService.queryForList(params)
        def newList = [["value": '', "label": '===请选择===']]
        list?.each {
            def map = [:]
            map.put("value", it.VALUE)
            map.put("label", it.LABEL)
            newList.add(map)
        }

        render(JsonOutput.toJson(newList))
    }
}